package cn.simon.free.redis.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.concurrent.TimeUnit;

/**
 * jedis 配置文件
 * 1. redis 启动检测
 * 2. RedisTemplate 配置
 *
 * @author simon
 * @date 2020/11/18 14:04
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@Configuration
@ConditionalOnProperty("spring.redis.host")
public class JedisConfig {

    private final RedisProperties redisProperties;

    public JedisConfig(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
    }

    @Bean(destroyMethod = "destroy")
    public JedisPool jedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        RedisProperties redisProperties = this.redisProperties;
        config.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().getSeconds());
        config.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
        config.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
        config.setTestWhileIdle(true);
        config.setTestOnBorrow(true);
        config.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
        config.setTestOnReturn(true);
        config.setMinEvictableIdleTimeMillis(TimeUnit.MINUTES.toMillis(10L));
        JedisPool jedisPool = new JedisPool(config.clone(), redisProperties.getHost(), redisProperties.getPort(),
                redisProperties.getTimeout().getNano(), redisProperties.getPassword());
//        Optional.ofNullable(jedisPool.getResource().asking()).ifPresent(v -> log.info("Redis connection check is successful"));
        return jedisPool;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // Json序列化配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // String序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // key 序列化方式
        template.setKeySerializer(stringRedisSerializer);

        // hash的key序列化方式
        template.setHashKeySerializer(stringRedisSerializer);

        // value的序列化方式（json序列化）
        template.setValueSerializer(jackson2JsonRedisSerializer);

        // hash的value序列化方式（json序列化）
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
